package com.solidtrust.common;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class FreePersister {

	@PersistenceContext
	private EntityManager em;
	
	@Transactional(readOnly = false)
	public void save(Object log){
		em.persist(log);
	}
	
	@Transactional(readOnly = false)
	public void merge(Object log){
		em.merge(log);
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void save1(Object log){
		em.persist(log);
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void saveBatch(List<?> items){
		for (Object f : items) {
			em.merge(f);
		}
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void deleBatch(List<?> items){
		for (Object f : items) {
			em.remove(f);
		}
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public int executeNativeQuery(String ql){
		return em.createNativeQuery(ql).executeUpdate();
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public int executeQuery(String ql){
		return em.createQuery(ql).executeUpdate();
	}
}
